//
// Created by benxb on 2022/3/22.
//

#include <gtest/gtest.h>
#include <mem/data_table_manager.h>
#include <catelog/table.h>
#include <vector>
#include <catelog/column.h>
#include <cstdio>
#include <memory>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <access/database.h>
#include <timer.h>

const std::string table = "KRL";

TEST(TableManagerTest, Q1) {
    benchmark::Timer timer;
    timer.Start();
    auto db = std::make_shared<benxdb::Database>();
    benxdb::PointCloud<benxdb::PointXYZ> res;
    benxdb::CircleQuery query(table);
    query.Around(50, 100, 20);
    db->QueryPointCloud(query, res);
    std::cout << res.Size() << std::endl;
    timer.StopAndPrint("Q1");
}

TEST(TableManagerTest, Q2) {
    benchmark::Timer timer;
    timer.Start();
    auto db = std::make_shared<benxdb::Database>();
    benxdb::PointCloud<benxdb::PointXYZ> res;
    benxdb::CircleQuery query(table);
    query.Around(0, 50, 10);
    db->QueryPointCloud(query, res);
    timer.StopAndPrint("Q2");
}

TEST(TableManagerTest, Q3) {
    benchmark::Timer timer;
    timer.Start();
    auto db = std::make_shared<benxdb::Database>();
    benxdb::PointCloud<benxdb::PointXYZ> res;
    benxdb::RectangleQuery query(table);
    query.SetRectangle(40, 70, 200, 240);
    db->QueryPointCloud(query, res);
    timer.StopAndPrint("Q3");
}

TEST(TableManagerTest, Q4) {
    benchmark::Timer timer;
    timer.Start();
    auto db = std::make_shared<benxdb::Database>();
    benxdb::PointCloud<benxdb::PointXYZ> res;
    benxdb::RectangleQuery query(table);
    query.SetRectangle(-20, 60, 20, 100);
    db->QueryPointCloud(query, res);
    timer.StopAndPrint("Q4");
}

TEST(TableManagerTest, Q5) {
    benchmark::Timer timer;
    timer.Start();
    auto db = std::make_shared<benxdb::Database>();
    std::vector<benxdb::Tuple> res;
    benxdb::QueryPhrase query(table);
    query.Select({"x", "y", "z"});
    query.Where("sweep", benxdb::EQUAL, benxdb::Value(benxdb::INTEGER, 1));
    db->Query(query, res);
    timer.StopAndPrint("Q5");
}

TEST(TableManagerTest, Q6) {
    benchmark::Timer timer;
    timer.Start();
    auto db = std::make_shared<benxdb::Database>();
    std::vector<benxdb::Tuple> res;
    benxdb::QueryPhrase query(table);
    query.Select({"x", "y", "z"});
    query.Where("sweep", benxdb::EQUAL, benxdb::Value(benxdb::INTEGER, 10));
    db->Query(query, res);
    timer.StopAndPrint("Q6");
}

TEST(TableManagerTest, Q7) {
    benchmark::Timer timer;
    timer.Start();
    auto db = std::make_shared<benxdb::Database>();
    std::vector<benxdb::Tuple> res;
    benxdb::QueryPhrase query(table);
    query.Select({"x", "y", "z"});
    query.Where("timestamp", benxdb::GREATER_EQUAL, benxdb::Value(benxdb::BIGINT, 1317617734934675));
    query.Where("timestamp", benxdb::LESS_EQUAL, benxdb::Value(benxdb::BIGINT, 1317617738252331));
    db->Query(query, res);
    timer.StopAndPrint("Q7");
}

TEST(TableManagerTest, Q8) {
    benchmark::Timer timer;
    timer.Start();
    auto db = std::make_shared<benxdb::Database>();
    std::vector<benxdb::Tuple> res;
    benxdb::QueryPhrase query(table);
    query.Select({"x", "y", "z"});
    query.Where("classification", benxdb::EQUAL, benxdb::Value(benxdb::INTEGER, 10));
    db->Query(query, res);
    timer.StopAndPrint("Q8");
}

TEST(TableManagerTest, Q9) {
    benchmark::Timer timer;
    timer.Start();
    auto db = std::make_shared<benxdb::Database>();
    std::vector<benxdb::Tuple> res;
    benxdb::QueryPhrase query(table);
    query.Select({"x", "y", "z"});
    query.SetOr();
    query.Where("classification", benxdb::EQUAL, benxdb::Value(benxdb::INTEGER, 80));
    query.Where("classification", benxdb::EQUAL, benxdb::Value(benxdb::INTEGER, 81));
    query.Where("classification", benxdb::EQUAL, benxdb::Value(benxdb::INTEGER, 99));
    db->Query(query, res);
    timer.StopAndPrint("Q9");
}

int main(int argc, char **argv) {
    testing::InitGoogleTest(&argc,argv);
    return RUN_ALL_TESTS();
}